package cn.tuling.aop;

import cn.tuling.service.OrderService;
import cn.tuling.service.OrderServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDynamicProxy {
    public static void main(String[] args) {
        OrderServiceImpl target = new OrderServiceImpl();

        Object proxy = Proxy.newProxyInstance(JdkDynamicProxy.class.getClassLoader(), new Class[]{OrderService.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("before...");
                Object result = method.invoke(target, args);
                System.out.println("after...");
                return result;
            }
        });

        OrderService proxy1 = (OrderService) proxy;
        proxy1.test();
    }
}
